package com.inxedu.os.edu.controller.comment;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.inxedu.os.common.PageEntity;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.inxedu.os.edu.pojo.CommentParam;
import com.inxedu.os.edu.pojo.CommentResult;
import com.inxedu.os.edu.pojo.EduComment;
import com.inxedu.os.edu.service.comment.CommentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@Api(tags = "评论管理")
public class CommentController {

    @Autowired
    private CommentService commentService;
    /**
     * 评论管理页面跳转，查询及模糊查询
     * @param modelAndView
     * @return  缺少pageNum参数传入
     */
    @ApiOperation(value = "评论管理页面跳转",notes = "跳转到评论管理页面,并且进行模糊查询")
    @ApiImplicitParams({@ApiImplicitParam(name = "page",dataType = "int",required = false,value = "分页显示需要的页数",defaultValue = "1",paramType = "qury"),
    @ApiImplicitParam(name = "email",dataType = "String",value = "邮箱模糊查询参数",required = false,defaultValue = "null",paramType = "qury"),
    @ApiImplicitParam(name = "content",dataType = "String",value = "邮箱内容模糊查询参数",required = false,defaultValue = "null",paramType = "qury"),
    @ApiImplicitParam(name = "type",dataType = "Integer",value = "模糊查询类型，为零时不使用此参数",required = false,defaultValue = "0",paramType = "qury"),
    @ApiImplicitParam(name = "beginCreateTime",dataType = "String",value = "模糊查询开始时间参数",required = false,defaultValue = "null",paramType = "qury"),
    @ApiImplicitParam(name = "endCreateTime",dataType = "String",value = "模糊查询结束时间参数",required = false,defaultValue = "null",paramType = "qury")})
    @RequestMapping(value = "admin/comment/query",method = {RequestMethod.GET,RequestMethod.POST})
    public ModelAndView toCommentPage(Integer page, HttpServletRequest httpServletRequest, String email, String content, @RequestParam(defaultValue = "0") Integer type, String beginCreateTime, String endCreateTime, ModelAndView modelAndView){
        if (page == null){
            PageHelper.startPage(1,10);
        }else {
            PageHelper.startPage(page,10);
        }
        System.out.println();
        Date startDate = null;
        Date endDate = null;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if(beginCreateTime != null&&!beginCreateTime.isEmpty()){
            try {
                startDate = format.parse(beginCreateTime);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        if(endCreateTime != null&&!endCreateTime.isEmpty()){
            try {
                endDate = format.parse(endCreateTime);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        CommentParam CommentParam = new CommentParam();
        CommentParam.setType(type);
        CommentParam.setContent(content);
        CommentParam.setEmail(email);
        CommentParam.setBeginCreateTime(startDate);
        CommentParam.setEndCreateTime(endDate);
        List<CommentResult> commentResults = commentService.selectCommentByMessage(email,content,type,startDate,endDate);
        PageInfo<CommentResult> info = new PageInfo<>(commentResults);
        httpServletRequest.setAttribute("comment",CommentParam);
        httpServletRequest.setAttribute("commentList",commentResults);
        httpServletRequest.setAttribute("page",info);
        modelAndView.setViewName("view/inxedu/admin/comment/comment-list");
        return  modelAndView;
    }


    /**
     * 删除评论请求
     * @param commentId
     * @return
     */
    @ApiOperation(value = "删除评论",notes = "删除指定ID的评论")
    @ApiImplicitParam(name = "commentId",dataType = "Integer",value = "传入指定将要删除的评论Id",required = true,paramType = "path")
    @PostMapping(value = "admin/comment/del/{commentId}")
    @ResponseBody
    public int delCommentById(@PathVariable Integer commentId){
        if(commentService.deleteCommentByCommentId(commentId)==1){
            return 200;
        }
        return 500;
    }

    /**
     * 修改评论页面请求
     * @param httpServletRequest
     * @param commentId
     * @param modelAndView
     * @return
     */
@ApiOperation(value = "修改评论页面请求",notes = "查询指定评论数据后返回页面")
@ApiImplicitParam(name = "commentId",value = "传入需要显示的指定评论Id",dataType = "Integer",required = true,paramType = "path")
    @GetMapping("admin/commentreply/info/{commentId}")
    public ModelAndView changeCommentPage(HttpServletRequest httpServletRequest,@PathVariable Integer commentId,ModelAndView modelAndView){
        CommentResult commentResults = commentService.selectCommentByCommentId(commentId);
        httpServletRequest.setAttribute("comment",commentResults);
        modelAndView.setViewName("view/inxedu/admin/comment/comment_info");
        return  modelAndView;
    }

    /**
     * 修改评论信息请求
     * @param eduComment
     * @return
     */
    @ApiOperation(value = "修改评论信息请求",notes = "讲需要修改的评论信息传入进行修改")
    @PostMapping("admin/commentreply/update")
    public String changeComment(EduComment eduComment){
        commentService.updateCommentMessageByCommentId(eduComment);
        return "redirect:/admin/comment/query";
    }

    /**
     * 回复列表请求
     * @param httpServletRequest
     * @param modelAndView
     * @param pCommentId   缺少pageNum参数传入
     * @return
     */
    @ApiOperation(value = "回复列表查询请求",notes = "查询指定评论的回复评论")
    @ApiImplicitParam(name = "pCommentId",value = "传入要查询回复列表的评论的Id,将会根据传入Id为父Id进行回复列表查询",dataType = "String",required = true,paramType = "qury")
    @GetMapping("admin/commentreply/query")
    public ModelAndView replyComment(Integer page,HttpServletRequest httpServletRequest,ModelAndView modelAndView,Integer pCommentId){
        if (page == null){
            PageHelper.startPage(1,10);
        }else {
            PageHelper.startPage(page,10);
        }
        List<CommentResult> commentResults = commentService.selectCommentByPComentId(pCommentId);
        PageInfo<CommentResult> info = new PageInfo<>(commentResults);
        modelAndView.setViewName("view/inxedu/admin/comment/comment-reply-list");
        httpServletRequest.setAttribute("commentList",commentResults);
        httpServletRequest.setAttribute("page",info);
        return modelAndView;
    }



}
